import DeviceCategoryApi from "../../api/broadcast/DeviceCategoryApi";

const useDeviceCategoryStore = defineStore(
  'deviceCategory',
  {
    state: () => ({
      categories: []
    }),
    actions: {
      // 修改布局设置
      list(isForce) {
        return new Promise((resolve, reject) => {
          if (isForce || !this.categories || !this.categories.length) {
            DeviceCategoryApi.list().then((data) => {
              this.categories = data
              resolve(data)
            }, (res) => reject(res))
          } else {
            resolve(this.categories)
          }
        })
      },
      addOrUpdate(category) {
        const index = this.categories.findIndex(item => item.id === category.id)
        if (index < 0) {
          this.categories.push(category)
        } else {
          this.categories.splice(index, 1, category)
        }
      }
    }
  })

export default useDeviceCategoryStore
